package cn.zyq.charging.cost.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * Redis 配置类
 */
@Configuration
public class RedisConfiguration {

    @Bean//本注解标记的方法会new对象并放入到spring容器中管理(其他类中就可以用@Autiwired或@Resource注入此对象了)
    public RedisTemplate<String, Serializable> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
        //设置连接工厂
        redisTemplate.setConnectionFactory(connectionFactory);
        //设置key的序列化器(cost服务中存入到redis中的key都是String类型)
        redisTemplate.setKeySerializer(RedisSerializer.string());
        //设置value的序列化器(cost服务中存入到redis中的value都是json类型)
        redisTemplate.setValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }
}
